package com.jic.manager.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;

import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.MestoreAddress;
import com.jic.manager.mapper.MestoreAddressMapper;
import com.jic.manager.service.MestoreAddressService;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;

import com.jic.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 店铺地址
 * @author : 
 * @email : 
 * @since : 2020-02-26 09:39:59
 * @version : v1.0.0
 */
@Service
public class MestoreAddressServiceImpl implements MestoreAddressService{

    @Resource
    private MestoreAddressMapper mestoreAddressMapper;

    @Override
    public long insert(MestoreAddress mestoreAddress) {
        long flag = mestoreAddressMapper.insert(mestoreAddress);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return mestoreAddressMapper.delete(id);
    }

    @Override
    public long update(MestoreAddress mestoreAddress) {
        return mestoreAddressMapper.update(mestoreAddress);
    }

    @Override
    public MestoreAddress queryByPrimaryKey(Long id){
        return mestoreAddressMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MestoreAddress> query(MestoreAddress mestoreAddress){
        return mestoreAddressMapper.query(mestoreAddress);
    }

    @Override
    public PageResult<MestoreAddress> queryPage(MestoreAddress mestoreAddress, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<MestoreAddress> list = mestoreAddressMapper.queryPage(startIndex, pageQuery.getLimit(), mestoreAddress);
        long count = mestoreAddressMapper.queryPageCount(mestoreAddress);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Transactional
    @Override
    public RestResult<String> saveAddressInfo(MestoreAddress memStoreAddress) {
        try{
            Long id =memStoreAddress.getId();
            // TODO null放判断符前面 走正向流程，别取反
            if(id != null && id != 0){
                if(memStoreAddress.getAddressType()==0){//如果是发货地址
                    if(memStoreAddress.getDefaultaddress()==0){//如果勾选了默认地址
                        MestoreAddress memStoreInfo = new MestoreAddress();
                        memStoreInfo.setAddressType(0L);
                        memStoreInfo.setDefaultaddress(1L);
                        memStoreInfo.setStoreId(memStoreAddress.getStoreId());
                        mestoreAddressMapper.update(memStoreInfo);//先修改此地址类型下的所有地址默认状态
                        mestoreAddressMapper.update(memStoreAddress);
                    }
                    if (memStoreAddress.getDefaultaddress()==1){//如果没有勾选
                        mestoreAddressMapper.update(memStoreAddress);
                    }
                }
                if(memStoreAddress.getAddressType()==1){//如果是退货地址
                    if(memStoreAddress.getDefaultaddress()==0){//如果勾选了默认地址
                        MestoreAddress memStoreInfos = new MestoreAddress();
                        memStoreInfos.setAddressType(1L);
                        memStoreInfos.setDefaultaddress(1L);
                        memStoreInfos.setStoreId(memStoreAddress.getStoreId());
                        mestoreAddressMapper.update(memStoreInfos);//先修改此地址类型下的所有地址默认状态
                        mestoreAddressMapper.update(memStoreAddress);
                    }
                    if(memStoreAddress.getDefaultaddress()==1){//如果没有勾选
                        mestoreAddressMapper.update(memStoreAddress);
                    }
                }
            }else {
                if(memStoreAddress.getAddressType()==0){//判断地址类型
                    if(memStoreAddress.getDefaultaddress()==0){//判断是否勾选
                        MestoreAddress memStoreInfo = new MestoreAddress();
                        memStoreInfo.setAddressType(0L);
                        memStoreInfo.setDefaultaddress(1L);
                        memStoreInfo.setStoreId(memStoreAddress.getStoreId());
                        mestoreAddressMapper.update(memStoreInfo);//先修改此地址类型下的所有地址默认状态
                        memStoreAddress.setDefaultaddress(0L);
                        memStoreAddress.setCreateTime(new Date());
                        memStoreAddress.setDeleteFlag(0);
                        mestoreAddressMapper.insert(memStoreAddress);
                    }
                   else { //如果没有勾选则正常新增
                        mestoreAddressMapper.insert(memStoreAddress);

                    }

                }
                else{
                    if(memStoreAddress.getDefaultaddress()==0){//判断是否勾选
                        MestoreAddress memStoreInfos = new MestoreAddress();
                        // TODO  为什么不用 1L
                        memStoreInfos.setAddressType(1L);
                        memStoreInfos.setDefaultaddress(1L);
                        memStoreInfos.setStoreId(memStoreAddress.getStoreId());
                        mestoreAddressMapper.update(memStoreInfos);//先修改此地址类型下的所有地址默认状态
                        memStoreAddress.setDefaultaddress(0L);
                        memStoreAddress.setCreateTime(new Date());
                        memStoreAddress.setDeleteFlag(0);
                        mestoreAddressMapper.insert(memStoreAddress);
                    }
                      else {
                        mestoreAddressMapper.insert(memStoreAddress);
                    }
                }
            }
            return RestResult.success("成功");
        }catch (Exception e){
            // TODO
           e.printStackTrace();
           return RestResult.error("9999",e.getLocalizedMessage());
        }
    }

    @Override
    public RestResult<String> deleteAddressInfo(long id) {
        try{
            mestoreAddressMapper.delete(id);
            return RestResult.success("成功");
        } // TODO 不用换号
        catch (Exception e){
            // TODO
          e.printStackTrace();
          return RestResult.error("9999",e.getLocalizedMessage());
        }
    }
}